﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Data;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Command;
using GoldDigger.AppServices;
using GoldDigger.Models;
using GoldDigger.Data;

namespace GoldDigger.ViewModels
{
    class DataSummaryViewModel : ViewModelBase
    {
        private ICommand _previewDataCommand;
        private ICommand _removeAttributesCommand;
        private ICommand _updateDataSetCommand;
        private ICommand _featureSelectionCommand;
        private ICommand _associationRuleCommand;

        private MainService _app;
        private string _classCount;
        private ObservableCollection<AttributeModel> _attributes;
        private RemoveAttributeWindow _win;

        public DataSummaryViewModel(MainService ms)
        {
            _app = ms;
            _classCount = "";
        }

        public DataTable MyDataSet
        {
            get
            {
                return _app.AppData.SourceDT;
            }
        }

        public int AttributeCount
        {
            get
            {
                return _app.AppData.SourceDT.Columns.Count;
            }
        }

        public ObservableCollection<AttributeModel>Attributes
        {
            get
            {
                return _attributes;
            }
            set
            {
                _attributes = value;
                RaisePropertyChanged("Attributes");
            }
        }

        public int InstanceCount
        {
            get
            {
                return _app.AppData.SourceDT.Rows.Count;
            }
        }

        public string ClassCount
        {
            get
            {
                _classCount = "";
                var distinctValues = (from r in _app.AppData.SourceDT.AsEnumerable() select r["Category"]).Distinct().ToList();
                foreach (var i in distinctValues)
                {
                    var count = 0;
                    var allvalues = (from r in _app.AppData.SourceDT.AsEnumerable() select r["Category"]).ToList();
                    foreach (var j in allvalues)
                        if (j.Equals(i))
                            count++;
                    _classCount += i+": "+count+" values. "; 
                }
                return _classCount;
            }
        }

        public int DataClassCount
        {
            get
            {
                var distinctValues = (from r in _app.AppData.SourceDT.AsEnumerable() select r["Category"]).Distinct().ToList();
                return distinctValues.Count;
            }
        }

        public ICommand PreviewDataCommand
        {
            get
            {
                return _previewDataCommand ?? (_previewDataCommand = new RelayCommand(GoToPreview));
            }
        }

        public void GoToPreview()
        {
            Messenger.Default.Send(new NotificationMessage<bool>(true, "Wait"));
            var win = new DataSummaryWindow();
            win.DataContext = this;
            Messenger.Default.Send(new NotificationMessage<bool>(false, "Wait"));

            win.ShowDialog();
        }

        public ICommand RemoveAttributesCommand
        {
            get
            {
                return _removeAttributesCommand ?? (_removeAttributesCommand = new RelayCommand(GoToAttributeRemoval));
            }
        }

        public void GoToAttributeRemoval()
        {
            _attributes = new ObservableCollection<AttributeModel>(_app.AppData.AttributeList);
            RaisePropertyChanged("Attributes");
            _win = new RemoveAttributeWindow();
            _win.DataContext = this;
            _win.ShowDialog();
        }

        public ICommand UpdateDataSetCommand
        {
            get
            {
                return _updateDataSetCommand ?? (_updateDataSetCommand = new RelayCommand(UpdateDataSet));
            }
        }

        public void UpdateDataSet()
        {
            foreach (AttributeModel model in _attributes)
            {
                if (model.ToRemove)
                {
                    _app.AppData.AttributeList.Remove(model);
                    _app.AppData.SourceDT.Columns.Remove(model.AttributeName);
                }
            }

            _attributes = new ObservableCollection<AttributeModel>(_app.AppData.AttributeList);
            RaisePropertyChanged("Attributes");
            RaisePropertyChanged("AttributeCount");
            RaisePropertyChanged("InstanceCount");
            RaisePropertyChanged("DataClassCount");
            RaisePropertyChanged("ClassCount");
        }

        public ICommand FeatureSelectionCommand
        {
            get
            {
                return _featureSelectionCommand ?? (_featureSelectionCommand = new RelayCommand(GoToFeatureSelection));
            }
        }

        public void GoToFeatureSelection()
        {
            Messenger.Default.Send(new NotificationMessage<ViewModelBase>(new FeatureSelectionViewModel(_app), "ChangeVM"));
        }

        public ICommand AssociationRuleCommand
        {
            get
            {
                return _associationRuleCommand ?? (_associationRuleCommand = new RelayCommand(GoToAssociationRule));
            }
        }

        public void GoToAssociationRule()
        {
            _app.CommandCallSource = CallSourceEnum.CallSource.Full;
            Messenger.Default.Send(new NotificationMessage<ViewModelBase>(new AssociationRuleViewModel(_app), "ChangeVM"));
        }
    }
}
